using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Web.Mvc;
using etcetera.Mvc.ActionResults;
using etcetera.Mvc.Syndication;
using Samples.Models;

namespace Samples.Controllers
{
    /// <summary>
    /// Sample controller to demo the FeedResult ActionTypes.
    /// </summary>
    public class FeedController : Controller
    {
        //
        // GET: /Feed/Atom
        //
        public FeedResult Atom()
        {
            SyndicationFeed feed = new SyndicationFeed("Atom Feed",
                                                       "This is a test Atom feed",
                                                       new Uri("http://localhost:8888/feed/atom"),
                                                       "AtomFeedId",
                                                       DateTime.Now);

            // Instead of creating a List<SyndicationItem>, use this helper class which allows you to pass your data to be in the feed.
            feed.Items = new SyndicationItemList<BlogPost>(GetBlogPosts(),
                                                           x => x.Title,
                                                           x => x.Text,
                                                           x => new Uri("http://localhost:8888/Posts/" + x.Id), // this property specifier can point to a string or URI
                                                           x => x.Id.ToString(),
                                                           x => DateTime.Now);

            return new AtomResult { Feed = feed };
        }

        //
        // GET: /Feed/Rss
        //
        public FeedResult Rss()
        {
            SyndicationFeed feed = new SyndicationFeed("RSS Feed",
                                                       "This is a test RSS feed",
                                                       new Uri("http://localhost:8888/feed/rss"),
                                                       "RssFeedId",
                                                       DateTime.Now);

            // Instead of creating a List<SyndicationItem>, use this helper class which allows you to pass your data to be in the feed.
            feed.Items = new SyndicationItemList<BlogPost>(GetBlogPosts(),
                                                           x => x.Title,
                                                           x => x.Text,
                                                           x => new Uri("http://localhost:8888/Posts/" + x.Id), // this property specifier can point to a string or URI
                                                           x => x.Id.ToString(),
                                                           x => DateTime.Now);

            return new RssResult { Feed = feed };
        }

        /// <summary>
        /// Gets the sample blog posts. this is just for demo purposes. The actual data would come from your repository / DAL / service / whatever.
        /// </summary>
        /// <returns></returns>
        private IEnumerable<BlogPost> GetBlogPosts()
        {
            IList<BlogPost> posts = new List<BlogPost>();

            for (int i = 1; i <= 10; i++)
            {
                BlogPost post = new BlogPost
                                    {
                                        Id = i,
                                        Title = "Post title " + i,
                                        Text = "Post text " + i,
                                        Date = DateTime.Now
                                    };
                posts.Add(post);
            }

            return posts;
        }
    }
}
